package com.yun.datastructure.linear.list.linkedlist;

import java.util.Iterator;
import java.util.function.Consumer;

/**
 * 单向链表(带哨兵)
 */
public class SinglyLinkedListSentinel implements Iterable<Integer> {
    //节点类

    static class Node {
        int value;//值
        Node next;//下一个节点指针

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }

    }

    private Node head = new Node(-1, null);//头指针

    private Node findNode(int index) {
        int i = -1;
        for (Node p = head; p != null; p = p.next, i++) {
            if (i == index) {
                return p;
            }
        }
        return null;
    }

    public void addFirst(int value) {
        //1.链表为空
        //head = new Node(value, null);
        //2.链表非空
        insert(0, value);
    }

    /*
     * 删除第一个节点
     */
    public void removeFirst() throws IllegalArgumentException {
        remove(0);
    }

    public void addLast(int value) {
        Node last = findLast();
        last.next = new Node(value, null);

    }

    private Node findLast() {
        Node p;
        for (p = head; p.next != null; p = p.next) {
        }
        return p;
    }

    /*
     * 向索引位置插入
     */
    public void insert(int index, int value) throws IllegalArgumentException {
        Node prev = findNode(index - 1);
        if (prev == null) {
            throw IllegaIndex(index);
        }
        prev.next = new Node(value, prev.next);
    }

    public void remove(int index) throws IllegalArgumentException {
        Node perv = findNode(index - 1);
        if (perv == null) {
            throw IllegaIndex(index);
        }
        Node remove = perv.next;
        if (remove == null) {
            throw IllegaIndex(index);
        }
        perv.next = remove.next;
    }

    public int get(int index) throws IllegalArgumentException {
        Node node = findNode(index);
        if (node == null || node == head) {
            throw IllegaIndex(index);
        }
        return node.value;
    }

    //迭代器遍历
    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node p = head.next;

            @Override
            public boolean hasNext() {//是否有下一个元素
                return p != null;
            }

            @Override
            public Integer next() {//返回当前值，并指向下一个元素
                int temp = p.value;
                p = p.next;
                return temp;
            }
        };
    }

    //遍历链表
    public void loop(Consumer<Integer> consumer) {
        Node p = head.next;
        while (p != null) {
            consumer.accept(p.value);
            p = p.next;
        }
    }

    public void loop2(Consumer<Integer> consumer) {
        for (Node p = head.next; p != null; p = p.next) {
            consumer.accept(p.value);
        }
    }

    private IllegalArgumentException IllegaIndex(int index) {
        return new IllegalArgumentException(
                String.format("index [%d] 不合法%n", index));
    }
}
